package io.github.uicdb.et;

import io.github.uicdb.et.block.*;
import io.github.uicdb.et.block.multipart.impl.ForgeAltar;
import io.github.uicdb.et.block.multipart.impl.ForgeAltarBE;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.RotatedPillarBlock;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.NoteBlockInstrument;
import net.minecraft.world.level.material.MapColor;
import net.minecraft.world.level.material.PushReaction;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredBlock;
import net.neoforged.neoforge.registries.DeferredRegister;

import java.util.Map;
import java.util.function.Function;

public class ModBlocks {
    private static final DeferredRegister.Blocks BLOCKS = DeferredRegister.createBlocks(ElementalTheory.MODID);

    public static final DeferredBlock<Block> CLOUD_BLOCK = registerBlock("cloud_block", CloudBlock::new, BlockBehaviour.Properties.of()
            .strength(0.3F)
            .sound(SoundType.SNOW)
            .noOcclusion()
    );
    //祭坛（多方块）
    public static final DeferredBlock<Block> AVATOR = registerBlock("avator", AvatorBlock::new, BlockBehaviour.Properties.of()
            .strength(0.3F)
            .sound(SoundType.SNOW)
            .noOcclusion()
    );
    //锻造炉（多方块）
    public static final DeferredBlock<ForgeAltar> FORGE_ALTAR = registerBlock("forge_altar", ForgeAltar::new, BlockBehaviour.Properties.of()
            .strength(0.3F)
            .sound(SoundType.METAL)
            .noOcclusion()
    );
    //提取台
    public static final DeferredBlock<ExtractMachineBlock> EXTRACT_MACHINE = registerBlock("extract_machine", ExtractMachineBlock::new, BlockBehaviour.Properties.of()
            .strength(0.3F)
            .sound(SoundType.METAL)
            .noOcclusion()
    );
    //聚合台
    public static final DeferredBlock<ElementMachineBlock> ELEMENT_MACHINE = registerBlock("element_machine", ElementMachineBlock::new, BlockBehaviour.Properties.of()
            .strength(0.3F)
            .sound(SoundType.METAL)
            .noOcclusion()
    );
    //灌注台
    public static final DeferredBlock<TrunInMachineBlock> TURN_IN_MACHINE = registerBlock("turn_in", TrunInMachineBlock::new, BlockBehaviour.Properties.of()
            .strength(0.3F)
            .sound(SoundType.METAL)
            .noOcclusion()
    );
    //息壤
    public static final DeferredBlock<EarthFarmland> EARTH_FARMLAND = registerBlock("earth_farmland", EarthFarmland::new,BlockBehaviour.Properties.of()
            .mapColor(MapColor.DIRT)
            .randomTicks()
            .strength(0.6F)
            .sound(SoundType.GRAVEL)
            .isViewBlocking((a,b,c)->true)
            .isSuffocating((a,b,c)->true)
    );
    //庚金
    public static final DeferredBlock<Block> METAL_ORE = registerBlock("metal_ore", Block::new, BlockBehaviour.Properties.of()
            .strength(1.2F)
            .sound(SoundType.NETHER_ORE)
            .pushReaction(PushReaction.IGNORE)
            .explosionResistance(1.0f)
            .noOcclusion()
    );
    //灵木
    public static final DeferredBlock<RotatedPillarBlock> WOOD_WOOD = registerBlock("wood_wood", p->log(MapColor.COLOR_CYAN, MapColor.COLOR_YELLOW), BlockBehaviour.Properties.of()
            .strength(0.4f)
            .sound(SoundType.WOOD)
            .explosionResistance(0.2f)
            .noOcclusion()
    );
    private static RotatedPillarBlock log(MapColor topMapColor, MapColor sideMapColor) {
        return new RotatedPillarBlock(BlockBehaviour.Properties.of().mapColor((p_152624_) -> {
            return p_152624_.getValue(RotatedPillarBlock.AXIS) == Direction.Axis.Y ? topMapColor : sideMapColor;
        }).instrument(NoteBlockInstrument.BASS).strength(2.0F).sound(SoundType.WOOD).ignitedByLava());
    }
    public static void init(IEventBus bus) {
        BLOCKS.register(bus);
    }

    /**
     * 同时注册方块和对应的物品
     *
     * @param key        id
     * @param properties 属性
     */
    public static DeferredBlock<Block> registerSimpleBlock(String key, BlockBehaviour.Properties properties) {
        var block = BLOCKS.registerSimpleBlock(key, properties);
        ModItems.ITEMS.registerSimpleBlockItem(key, block);
        return block;
    }

    /**
     * 同时注册方块和对应的物品
     *
     * @param key        id
     * @param func       方块
     * @param properties 属性
     */
    public static <T extends Block> DeferredBlock<T> registerBlock(String key, Function<BlockBehaviour.Properties, T> func, BlockBehaviour.Properties properties) {
        var block = BLOCKS.registerBlock(key, func, properties);
        ModItems.ITEMS.registerSimpleBlockItem(key, block);
        return block;
    }

}
